import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../views/login.vue'
// import Home from '../views/home.vue'
// import Welcome from '../views/welcome.vue'
// import Users from '../views/users'
// import Rights from '../views/rights/rightsList.vue'
// import Loles from '../views/rights/roles.vue'
// import Cate from '../views/goods/cate.vue'
// import Params from '../views/goods/params.vue'
// import Goods from '../views/goods/goods.vue'
// import Add from '../views/goods/add.vue'
// import OrderList from '../views/orders/list.vue'
// import Reports from '../views/reports/reports.vue'
const Login = () => import(/* webpackChunkName: "login" */ '../views/login.vue')
const Home = () => import(/* webpackChunkName: "home" */ '../views/home.vue')
const Welcome = () => import(/* webpackChunkName: "welcome" */ '../views/welcome.vue')
const Users = () => import(/* webpackChunkName: "users" */ '../views/users')
const Rights = () => import(/* webpackChunkName: "rights" */ '../views/rights/rightsList.vue')
const Loles = () => import(/* webpackChunkName: "loles" */ '../views/rights/roles.vue')
const Cate = () => import(/* webpackChunkName: "cate" */ '../views/goods/cate.vue')
const Params = () => import(/* webpackChunkName: "params" */ '../views/goods/params.vue')
const Goods = () => import(/* webpackChunkName: "goods" */ '../views/goods/goods.vue')
const Add = () => import(/* webpackChunkName: "add" */ '../views/goods/add.vue')
const OrderList = () => import(/* webpackChunkName: "orderList" */ '../views/orders/list.vue')
const Reports = () => import(/* webpackChunkName: "reports" */ '../views/reports/reports.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Loles
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: Goods
      },
      {
        path: '/goods/add',
        component: Add
      },
      {
        path: '/orders',
        component: OrderList
      },
      {
        path: '/reports',
        component: Reports
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 全局路由守卫,每一个路由跳转之前会出发
// 路由钩子函数
// 判断是否有token 有token 放行 没有判断是否在登录页 如果不是强制跳转
router.beforeEach((to, from, next) => {
  // console.log(from)
  // console.log(to)
  const token = sessionStorage.getItem('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') return next()
    next('/login')
  }
})
export default router
